# 定义Java版本和下载URL
$javaVersion = "21"
$javaUrl = "https://repo.huaweicloud.com/openjdk/21/openjdk-21_windows-x64_bin.zip"

# 定义下载路径和解压路径
$downloadPath = "$env:TEMP\openjdk-$javaVersion.zip"
$installPath = "C:\Program Files\OpenJDK"
$envPath = "C:\Program Files\OpenJDK\jdk-$javaVersion"

# 下载Java安装程序
Write-Host "Downloading Java..."
Invoke-WebRequest -Uri $javaUrl -OutFile $downloadPath

# 创建安装目录
Write-Host "Creating installation directory..."
New-Item -ItemType Directory -Path $installPath -Force

# 解压缩下载的Java安装程序
Write-Host "Extracting Java..."
Expand-Archive -Path $downloadPath -DestinationPath $installPath

# 删除下载的zip文件
Remove-Item -Path $downloadPath

# 配置环境变量
Write-Host "Configuring environment variables..."
[System.Environment]::SetEnvironmentVariable("JAVA_HOME", $envPath, [System.EnvironmentVariableTarget]::Machine)

$path = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
$newPath = "$path;$envPath\bin"
[System.Environment]::SetEnvironmentVariable("Path", $newPath, [System.EnvironmentVariableTarget]::Machine)

Write-Host "Java installation and configuration completed."